import hashlib
import struct
import time
import git
import config
import os
def intToBytes(num):
    return struct.pack('>Q', num)

def getCode(K):
    # 时间戳计数器
    T = int(time.time() / 300)
    # 计算 HMAC-SHA1，密钥为 K，消息为 T
    hmac = hashlib.sha1(K.encode())
    T1 = intToBytes(T)
    hmac.update(T1)
    HS = hmac.digest()

    # 取出最后个字节的低 4 位
    offset = HS[19] & 0xf

    # 将从 offset 开始的四个字节按大端组装为整数
    bytes = (HS[offset] & 0x7f) << 24 \
            | HS[offset + 1] << 16 \
            | HS[offset + 2] << 8 \
            | HS[offset + 3]

    # 整数转字符串，然后取出后六位
    code = str(bytes)[-6:]

    # 不足 6 位数则补 0
    code = code.rjust(6, '0')
    return code

def git_operation(title):
    repo = git.Repo(config.PROJECT_PATH)
    repo.git.config('user.name', 'linyouqiang')
    repo.git.config('user.email', '1786384951@qq.com')
    gitpull = git.cmd.Git(config.PROJECT_PATH)
    gitpull.pull()

    gitAdd = git.Repo(config.PROJECT_PATH).git
    gitAdd.add('.')

    gitCommit = git.Repo(config.PROJECT_PATH).git
    gitCommit.commit(m=f"新增 {title}页面")

    gitPush = git.Repo(config.PROJECT_PATH).git
    gitPush.push()
